home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14561 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: char* still alive after free ???
  5. Date: Mon, 15 Apr 96 18:12:11 GMT
  6. Organization: none
  7. Message-ID: <829591931snz@genesis.demon.co.uk>
  8. References: <317269EA.11BB93C2@studbox.uni-stuttgart.de>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <317269EA.11BB93C2@studbox.uni-stuttgart.de>
  15.            Markus.Heller@studbox.uni-stuttgart.de "Markus Heller" writes:
  16.  
  17. >Hi,
  18. >
  19. >I have a global variable :
  20. >
  21. >char *text=NULL;
  22. >
  23. >I want to use it to store different "strings" (at diffreent times).
  24. >E.g., if I want to sore 10 characters in text, I do a 
  25. >text=(char *) malloc(10*sizeof(char));
  26.  
  27. That's OK but why not write it simply as:
  28.  
  29.     text = malloc(10);
  30.  
  31. which is much clearer IMHO.
  32.  
  33. >When I want to use text to store 3 other characters, I first do a
  34. >free(text); text=NULL; and finally a
  35.  
  36. The text = NULL; is redundant.
  37.  
  38. >text=(char *) malloc(3*sizeof(char));
  39. >But to my surprise there are still the 4thh to 10th charcter of
  40. >text contained before the free(text)/malloc... ???
  41.  
  42. How can you tell? It is illegal to access beyond text[2] but clearly you
  43. have done so and have as a consequence invoked undefined behaviour. That
  44. means anything can happen including what you saw.
  45.  
  46. By calling free() you're telling the system that it can reuse the memory
  47. occupied by the object you passed a pointer to. It doesn't have to
  48. wipe that memory or make it inaccessible so you're just reading the
  49. last data that was put in it. You've no guarantees that that data won't have
  50. changed, it just hadn't in your case.
  51.  
  52. >This happens under linux, but why ?
  53.  
  54. That's just the way the Linux malloc functions worked in that case. When you
  55. malloc an object (such as an array of char in this case) its contents
  56. are indeterminate. You must write some data to it before you can legally
  57. read it. 
  58.  
  59. >(what I want to to is to get filenames from the user and then open those
  60. > files by a function to which I pass the file name..)
  61.  
  62. There's nothing here that stops you doing that.
  63.  
  64. -- 
  65. -----------------------------------------
  66. Lawrence Kirby | fred@genesis.demon.co.uk
  67. Wilts, England | 70734.126@compuserve.com
  68. -----------------------------------------
  69.